home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!zodiac!szh
- From: szh@zcon.com (Syed Zaeem Hosain)
- Subject: Re: A question on for loop
- Message-ID: <1996Mar14.041132.26035@zcon.com>
- Sender: szh@zcon.com (Syed Zaeem Hosain)
- Nntp-Posting-Host: zodiac
- Reply-To: szh@zcon.com
- Organization: Z Consulting Group
- References: <4i77ca$161@ccshst05.cs.uoguelph.ca>
- Date: Thu, 14 Mar 1996 04:11:32 GMT
-
- In article <4i77ca$161@ccshst05.cs.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) writes:
- >Phil Boyd (boydp@hdc-usa.com) wrote:
- >: > |>
- >: > |> for(i=0;i< 20; i++)
- >: > |> {
- >: > ...
- >: > |> if(condition==FALSE)
- >: > |> continue;
- >: > ...
- >: > |> }
- >
- >: OK, now for a newbie question just for my own edification:
- >: Inside the for loop - does "i" start off with the value 0 so that
- >: the loop has the potential to repeat up to 20 times (assuming that
- >: "condition" remains FALSE)?
- >
- >My novice's answer is yes, i starts off at 0 because the for statement
- >(i=0;i<20;i++) has i++ meaning that i is incremented after the
- >operation. If it were (i=0;i<20;++i) then i would start at 1 and run up
- >to 20. Is this correct?
-
- No. The ++i is still only done at the end of the loop, and i starts at
- a value of 0 due to the initial assignment. The best way to think of
- the looping code is as follows (temporarily forgetting about the
- condition check shown above):
-
- i = 0;
- while ( i < 20 )
- {
- [some code here]
- ...
-
- ++i; /* Can be i++ without any difference */
- }
-
- The ++i can be i++ without any difference *in this case shown* since
- you are not relying on the side effect of the assignment.
-
- Z
-
-
- --
- -------------------------------------------------------------------------
- | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- -------------------------------------------------------------------------
-